//
//  HotViewController.m
//  liwushuo
//
//  Created by qf1 on 16/4/15.
//  Copyright (c) 2016年 ZJH. All rights reserved.
//

#import "HotViewController.h"
#import "UIImageView+AFNetworking.h"
#import "HotDetailViewController.h"

@interface HotViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (assign,nonatomic)int offset;

@end

@implementation HotViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"正在加载";
    hud.detailsLabelText = @"请稍后";
    [hud show:YES];
    _offset = 0;
    self.title = @"热门";
    _dataArray = [[NSMutableArray alloc] init];
    [self createView];
    [self getData];
}

- (void)awakeFromNib {
    [self setTabBarImage];
}

- (void)setTabBarImage{
    UIImage * image = [UIImage imageNamed:@"hot2"];
//    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    [self.tabBarItem setSelectedImage:image];
}

- (void)createView{
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, theScreenWidth, theScreenHeight - 64 - 49) collectionViewLayout:layout];
    [_collectionView addHeaderWithTarget:self action:@selector(headerAction)];
    [_collectionView addFooterWithTarget:self action:@selector(footerAction)];
    _collectionView.headerRefreshingText = @"正在刷新哟";
    _collectionView.footerRefreshingText = @"正在加载哟";
    _collectionView.backgroundColor = [UIColor colorWithRed:245 / 255.0 green:245 / 255.0 blue:245 / 255.0 alpha:1];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:_collectionView];
}

- (void)headerAction{
    _offset = 0;
    [self getData];
}

- (void)footerAction{
    _offset += 20;
    [self getData];
}

- (void)getData{
    NetWorkManager * manager = [[NetWorkManager alloc] init];
    __weak HotViewController * weakself = self;
    [manager getHotDataWithUrl:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/items?gender=1&limit=20&offset=%d&generation=2",_offset] andBlock:^(id respond) {
        if (_offset == 0) {
            _dataArray = [[NSMutableArray alloc] initWithArray:respond];
            [_collectionView headerEndRefreshing];
        }else{
            [_dataArray addObjectsFromArray:respond];
            [_collectionView footerEndRefreshing];
        }
        [_collectionView reloadData];
        
        [MBProgressHUD hideAllHUDsForView:weakself.view animated:YES];
        
    }];
}

#pragma mark collectionview的代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dataArray.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HotItem * item = _dataArray[indexPath.row];
    static NSString * cellid = @"cell";
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellid forIndexPath:indexPath];
    [cell.layer setCornerRadius:5];
    [cell.layer setMasksToBounds:YES];
    
    cell.backgroundColor = [UIColor whiteColor];
    UIImageView * imageView = (UIImageView *)[cell.contentView viewWithTag:101];
    if (imageView == nil) {
        imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, (theScreenWidth - 30) / 2, (theScreenWidth - 30) / 2)];
        [cell.contentView addSubview:imageView];
        imageView.tag = 101;
    }
    UILabel * nameLabel = (UILabel *)[cell.contentView viewWithTag:102];
    if (nameLabel == nil) {
        nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, (theScreenWidth - 30) / 2 + 5, (theScreenWidth - 30) / 2 - 10, 40)];
        nameLabel.numberOfLines = 2;
        nameLabel.tag = 102;
        nameLabel.font = [UIFont systemFontOfSize:13];
        [cell.contentView addSubview:nameLabel];
    }
    UILabel * priceLabel = (UILabel *)[cell.contentView viewWithTag:103];
    if (priceLabel == nil) {
        priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, (theScreenWidth - 30) * 3 / 4 - 30, 75, 20)];
        priceLabel.tag = 103;
        priceLabel.textColor = [UIColor redColor];
        priceLabel.font = [UIFont systemFontOfSize:13];
        [cell.contentView addSubview:priceLabel];
    }
    UILabel * favouriteLabel = (UILabel *)[cell.contentView viewWithTag:104];
    if (favouriteLabel == nil) {
        favouriteLabel = [[UILabel alloc] initWithFrame:CGRectMake((theScreenWidth - 30) / 2 - 40, (theScreenWidth - 30) * 3 / 4 - 30, 40, 20)];
        favouriteLabel.numberOfLines = 2;
        favouriteLabel.tag = 104;
        favouriteLabel.textColor = [UIColor grayColor];
        favouriteLabel.font = [UIFont systemFontOfSize:12];
        [cell.contentView addSubview:favouriteLabel];
    }
    
    [imageView setImageWithURL:[NSURL URLWithString:item.cover_image_url]];
    nameLabel.text = item.name;
    priceLabel.text = item.price;
    float price = [item.favorites_count intValue] / 1000.0;
    favouriteLabel.text = [NSString stringWithFormat:@"%0.1fk",price];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((theScreenWidth - 30) / 2, (theScreenWidth - 30) * 3 / 4);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    HotDetailViewController * vc = [[HotDetailViewController alloc] init];
    HotItem * item = _dataArray[indexPath.row];
    vc.webStr = item.url;
    [self.navigationController pushViewController:vc animated:YES];
    
}

//返回列与列之间最小的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 0, 10);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated{
    [self.navigationController.navigationBar setHidden:NO];
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.translucent = NO;
    [self.tabBarController.tabBar setHidden:NO];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
